﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;
using System.Data.SqlClient;

namespace Lists
{
    public class KhoanMucPhieu : DoiTuong
    {
        #region Guid PhieuID property
        public Guid PhieuID
        {
            get
            {
                if (_DataRow["PhieuID"] is Guid) return (Guid)_DataRow["PhieuID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["PhieuID"] = value;
            }
        }
        #endregion

        #region Guid DichVuID property
        public Guid DichVuID
        {
            get
            {
                if (_DataRow["DichVuID"] is Guid) return (Guid)_DataRow["DichVuID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["DichVuID"] = value;
            }
        }
        #endregion

        #region decimal GiaBan property
        public decimal GiaBan
        {
            get
            {
                if (_DataRow["GiaBan"] is decimal) return (decimal)_DataRow["GiaBan"];
                return 0;
            }
            set
            {
                _DataRow["GiaBan"] = value;
            }
        }
        #endregion

        #region decimal ChietKhau property
        public decimal ChietKhau
        {
            get
            {
                if (_DataRow["ChietKhau"] is decimal) return (decimal)_DataRow["ChietKhau"];
                return 0;
            }
            set
            {
                _DataRow["ChietKhau"] = value;
            }
        }
        #endregion

        #region decimal ThanhTien property
        public decimal ThanhTien
        {
            get
            {
                if (_DataRow["ThanhTien"] is decimal) return (decimal)_DataRow["ThanhTien"];
                return 0;
            }
            set
            {
                _DataRow["ThanhTien"] = value;
            }
        }
        #endregion

        #region ThuTuKhoanMucTrongChungTu property
        public int ThuTuKhoanMucTrongChungTu
        {
            get
            {
                if (_DataRow["ThuTuKhoanMucTrongChungTu"] is int)
                    return (int)_DataRow["ThuTuKhoanMucTrongChungTu"];
                else
                    return 0;
            }
            set
            {
                _DataRow["ThuTuKhoanMucTrongChungTu"] = value;
            }
        }
        #endregion

        public KhoanMucPhieu(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucKhoanMucPhieu : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucKhoanMucPhieu _Instance;
        public static DanhMucKhoanMucPhieu Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucKhoanMucPhieu();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    try
                    {
                        UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                        _Instance.DataTable.Constraints.Add(uniqueConstraint);
                        _Instance.DataView.Sort = "Ma";
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Clinic_KhoanMucPhieu";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new KhoanMucPhieu((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public KhoanMucPhieu this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new KhoanMucPhieu(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public KhoanMucPhieu NewDoiTuong()
        {
            try
            {
                return new KhoanMucPhieu(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public KhoanMucPhieu GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new KhoanMucPhieu(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public KhoanMucPhieu GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucKhoanMucPhieu GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucKhoanMucPhieu Filter(string filterExpression)
        {
            try
            {
                DanhMucKhoanMucPhieu obj = new DanhMucKhoanMucPhieu();
                obj.DataView = new DataView(DanhMucKhoanMucPhieu.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public void LoadFromDB(Guid phieuID)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                LoadFromDB(cmd, phieuID);
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }
        public void LoadFromDB(SqlCommand cmd, Guid phieuID)
        {
            try
            {
                cmd.CommandText = string.Format("SELECT * FROM Clinic_KhoanMucPhieu WHERE PhieuID = '{0}' ORDER BY ThuTuKhoanMucTrongChungTu ASC", phieuID);

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable.Rows.Clear();
                da.Fill(DataTable);
                DataTable.AcceptChanges();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }


    }
}
